<template>
  <div class="setPatent">
    <navBar title="绑定专利申请系统" @onClickLeft="onClickLeft" />
    <div class="form">
      <van-form ref="form" >
        <van-field
          class="van-field-isRequired"
          v-model="phone"
          name="account"
          label="账号"
          disabled
          placeholder="请输入账号"
          :rules="[{ required: true, message: '请填写账号' }]"
        />
        <van-field
          class="van-field-isRequired"
          v-model="password"
          name="password"
          type="password"
          label="密码"
          placeholder="请输入当前用户密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
         name="checkbox"
         label="是否永久授权"
        >
          <template #input>
            <van-checkbox-group v-model="checkboxGroup" direction="horizontal">
              <van-checkbox name="1"></van-checkbox>
            </van-checkbox-group>
          </template>
        </van-field>
      </van-form>
      <div class="btn-box">
        <van-button round type="info" block @click="submit" class="mb15" :disabled="isDisabled">提交</van-button>
      </div>
    </div>
  </div>
</template>

<script>
import navBar from "@cb/navBar.vue";
import { Toast } from 'vant';
import {get,post} from "../../../../public/API/query"
export default {
  components: {
    navBar
  },
  data() {
    return {
      account: "",
      password: "",
      checkboxGroup:[],
      isDisabled:false
    };
  },
  created() {
    get('sysAuthPage',{},res=>{
      if (res.data.yes == 1) {
        this.$router.push("/user/autoapply")
      }else if(res.data.yes == -2){
        this.isDisabled = true
        Toast("您还未注册专利申请系统,无法绑定专利申请系统！")
      }
    })
  },
  deactivated() {
      this.$destroy(true); //销毁组件
  },
  computed:{
    phone(){
      return this.$store.state.user.phone
    }
  },
  methods: {
   submit(){
     if (!this.password) {
       Toast.fail('请输入当前账户密码')
       return
     }
     let params = {password:this.password}
     this.checkboxGroup.length==1?params.type = 1:params.type = -1
     get('sysAuthSub',params,res=>{
       console.log(res)
       if (res.code == 200) {
         Toast.success('验证成功')
         var patentSecret;
         if (res.data.type == 2) {
           patentSecret = res.data.data.patentSecret
         }
         this.$router.push({
          path: `/user/autoapply`,
          query: {
            patentSecret: patentSecret
          }
      });
       }else{
         Toast.fail(res.data.split(':')[1])
       }
     })
   },
   onClickLeft(){
       this.$router.go(-1)
   }
  }
};
</script>

<style lang="less">
.setPatent {
  height: 100%;
  width: 100%;

  .btn-box {
    width: 100%;
    box-sizing: border-box;
    padding: 15px 15px;
  }
  .form {
    margin-top: 80px;
    .van-form {
      .van-field {
        margin-bottom: 15px;
      }
    }
  }
}
</style>
